package com.fun.feifun.modules.sys.option.controller;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fun.feifun.modules.sys.option.dto.OptionGroupDto;
import com.fun.feifun.modules.sys.option.model.SysOptionItem;
import com.fun.feifun.modules.sys.option.service.ISysOptionItemService;
import com.fun.feifun.modules.sys.user.model.SysUser;
import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
import com.fun.feifun.modules.sys.option.service.ISysOptionGroupService;
import com.fun.feifun.modules.sys.option.model.SysOptionGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fun.feifun.base.result.Result;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;

import java.util.List;

/**
 * <p>
 * 系统-选项分组 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2023-07-03
 */
@RestController
@Api(value = "/sys/sys-option-group", tags = "系统-选项分组")
@Slf4j
@RequestMapping("/sys/sys-option-group")
    public class SysOptionGroupController {


    @Autowired
    private ISysOptionGroupService service;

    @Autowired
    private ISysOptionItemService itemService;

    /**
     * 添加数据
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者保存")
    public Result save(@RequestBody SysOptionGroup item){
        if(service.saveOrUpdate(item)){
         return Result.success("数据添加成功！");
        }
        return Result.err("数据添加成功！");
    }



    /**
     * 删除数据
     */
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public Result deleteItems(@RequestParam("ids") List<Long> ids){
        if(service.removeByIds(ids)){
            return Result.success("数据删除成功！");
        }
        return Result.err("数据删除失败");
    }



    /**
     * 获取全部数据
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取全部数据")
    public Result<List<SysOptionGroup>> getAll(){
        List<SysOptionGroup> all = service.list();
        return Result.success(all);
    }

    /**
     * 根据ID查找数据
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查找")
    public Result findById(@RequestParam("id") Long id){
        SysOptionGroup item = service.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }


    /**
     * 根据ID查找数据
     */
    @GetMapping("/getOptionByCode")
    @ApiOperation(value = "根据code查询选项")
    public Result getOptionByCode(@RequestParam("code") String code){

        QueryWrapper<SysOptionGroup> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(SysOptionGroup::getCode, code);
        SysOptionGroup item = service.getOne(queryWrapper);
        if(item==null){
            return Result.err("尚根据此code查询到数据");
        }
        QueryWrapper<SysOptionItem> itemQuery =new QueryWrapper<>();
        itemQuery.lambda().eq(SysOptionItem::getCode, code);
        List<SysOptionItem> list = itemService.list(itemQuery);

        OptionGroupDto dto=new OptionGroupDto();
        dto.setCode(item.getCode());
        dto.setTitle(item.getTitle());
        dto.setList(list);
        return Result.success(dto);
    }
}

